package com.hzsd.zengbanyi.biaodan_text.utils;

import java.util.ArrayList;

import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.widget.Toast;

/**
 * 百度离线地图
 * 
 * @author Administrator
 * 
 */
public class BaiduOfflineMap implements MKOfflineMapListener{
	private Context context;
	public MKOfflineMap mkOfflineMap;
	private ProgressDialog progressDialog;
	private int cityID;

	public BaiduOfflineMap(Context context) {
		this.context = context;
		mkOfflineMap = new MKOfflineMap();
		mkOfflineMap.init(this);
	}

	/**
	 * 离线更新百度地图
	 * 
	 * @param cityName
	 */
	public void updateMap(String cityName) {
		int cityId = getCityId(cityName);
		if (cityId==0) {
			return;
		}
		MKOLUpdateElement e = mkOfflineMap.getUpdateInfo(cityId);
		if (e==null) {
			DownloadMap(e);
		}else{
			if (e.update) {//可更新
				DownloadMap(e);
			}else{//最新
				Toast.makeText(context, "当前离线地图为最新版本", Toast.LENGTH_SHORT).show();
			}
		}
		
	}

	/**
	 * 根据城市名获取城市ID
	 * 
	 * @param cityName
	 * @return
	 */
	public int getCityId(String cityName) {
		ArrayList<MKOLSearchRecord> records = mkOfflineMap.searchCity(cityName);
		if (records == null || records.size() != 1) {
			return 0;
		}
		cityID = records.get(0).cityID;
		return cityID;
	}
	/**
	 * 下载离线地图
	 */
	public void DownloadMap(final MKOLUpdateElement e) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("离线地图更新");
		builder.setMessage("发现新版本离线地图,是否下载？");
		builder.setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		builder.setPositiveButton("下载", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				DownloadMapDialog();
				if (e==null) {
					mkOfflineMap.start(cityID);
				}else{
					mkOfflineMap.getUpdateInfo(cityID);
				}
			}
		});
		builder.create().show();
		
		
	}
	
	public void DownloadMapDialog(){
		if (progressDialog==null) {
			progressDialog = new ProgressDialog(context);
			progressDialog.setMessage("正在更新离线地图,请稍后...");
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//			progressDialog.setCancelable(false);
			progressDialog.setCanceledOnTouchOutside(false);
		}
		progressDialog.show();
		
	}
	@Override
	public void onGetOfflineMapState(int type, int state) {
		 switch (type) {
		 //当type为TYPE_DOWNLOAD_UPDATE时，state表示更新的城市ID.
         case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: {
             MKOLUpdateElement update = mkOfflineMap.getUpdateInfo(state);
             // 处理下载进度更新提示
             if (update != null) {
            	 progressDialog.setProgress(update.ratio);
            	 if (update.ratio==100) {
            		 progressDialog.dismiss();
				}
             }
         }
             break;
         case MKOfflineMap.TYPE_NEW_OFFLINE://当type为TYPE_NEW_OFFLINE时，state表示新安装的离线地图数目
             // 有新离线地图安装
             Log.d("OfflineDemo", String.format("add offlinemap num:%d", state));
             break;
         case MKOfflineMap.TYPE_VER_UPDATE:
             // 版本更新提示
             // MKOLUpdateElement e = mOffline.getUpdateInfo(state);

             break;
         default:
             break;
     }
		
	}

}
